# -*- coding: utf-8 -*-
#  @author  Bink
#  @date  2020/9/2 20:03
# @Email : 2641032316@qq.com

import numpy as np
import pandas as pd
import re
import time, datetime

# file_dorm = pd.read_table("D:/pra/Python/python-programming-project/src/resources/train/1.txt",
file_dorm = pd.read_table("D:/pra/Python/python-programming-project/src/resources/train/dorm_train.txt",
                          dtype=str,
                          delimiter=','
                          )
df_dorm = pd.DataFrame(file_dorm)
df_dorm.columns = ['id', 'actualTime', 'direction(0:in 1:out)']
# df_dorm_by_id = df_dorm.groupby('id')
# print(df_dorm_by_id.apply())
# print(df_dorm)

df_dorm['actualTime'] = (df_dorm['actualTime'].apply(lambda x:
                                         int(time.mktime(time.strptime(x, "%Y/%m/%d %H:%M:%S")))
                                         )
*
df_dorm['direction(0:in 1:out)'].apply(lambda x:
                                 -1 if int(x) == 0
                                 else
                                 1
                                 )
)

# df_dorm = df_dorm.iloc[:, :-1]

# print(df_dorm.groupby('id'))
# df_dorm2 = df_dorm.groupby('id').count().reset_index()['actualTime']
# df_dorm = df_dorm.groupby('id').sum().reset_index()
# df_dorm['actualTime'] = df_dorm['actualTime'].apply(lambda timeStamp:
#                                         time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(timeStamp))
#                                         )
# df_dorm['进出次数'] = df_dorm2
# df_dorm2 = df_dorm.groupby('id').count().reset_index()['id']
# df_dorm.to_excel("./out/dormInfo.xlsx", index=None)
# print(df_dorm)
# print(df_dorm2)
# print(type(df_dorm))
df_dorm['id'].astype(int)
# df_dorm3 = df_dorm.set_index(['id', 'actualTime'])['direction(0:in 1:out)'].sort_index()

df_dorm['date'] = [re.match(r'(\d{4}/\d{1,2}/\d{1,2})', dateStr).group() for dateStr in df_dorm['actualTime']]
df_dorm['month'] = [re.match(r'(\d{4}/\d{1,2})', dateStr).group() for dateStr in df_dorm['actualTime']]
df_dorm['year'] = [re.match(r'(\d{4})', dateStr).group() for dateStr in df_dorm['actualTime']]
# print(df_dorm['date'])
# df_dorm3 = df_dorm.groupby(['id', 'date']).sort_index()
df_dorm3 = df_dorm.set_index(['id', 'year', 'month', 'date'])[:].sort_index()
# df_dorm3 = df_dorm.applymap()
print(df_dorm3.head(100))
# df_dorm3 = df_dorm.set_index(['id', 'direction(0:in 1:out)'])['actualTime'].sort_index()
df_dorm3.to_json("./out/subsidyId.json")
# print(df_dorm3)
